package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.UserConstants;
import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysConfig;
import com.yuehuanghun.admin.core.service.ISysConfigService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.annotation.RepeatSubmit;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.web.BaseResource;
import com.yuehuanghun.mybatis.milu.data.Sort;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 参数配置 信息操作处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/config")
@SaCheckLogin
public class SysConfigController extends BaseResource {
	@Autowired
	private ISysConfigService configService;

	/**
	 * 获取参数配置列表
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@SaCheckPermission("system:config:list")
	public PageDTO<SysConfig> list(SysConfig config) {
		List<SysConfig> list = configService.getByExampleAndSort(config, Sort.desc(SysConfig::getConfigId), getPage());
		return toPage(list);
	}

	@SuppressWarnings("unchecked")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("system:config:export")
	public String export(SysConfig config) {
		List<SysConfig> list = configService.getByExampleAndSort(config, Sort.desc(SysConfig::getConfigId));
		ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
		return util.exportExcel(list, "参数数据");
	}

	/**
	 * 根据参数编号获取详细信息
	 */
	@GetMapping(value = "/{configId}")
	@SaCheckPermission("system:config:query")
	public SysConfig getInfo(@PathVariable Long configId) {
		return configService.getConfigById(configId);
	}

	/**
	 * 根据参数键名查询参数值
	 */
	@GetMapping(value = "/configKey/{configKey}")
	public String getConfigKey(@PathVariable String configKey) {
		return configService.getConfigByKey(configKey);
	}

	/**
	 * 新增参数配置
	 */
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping
	@RepeatSubmit
	@SaCheckPermission("system:config:add'")
	public void add(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			throw new ValidateException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		configService.insertConfig(config);
	}

	/**
	 * 修改参数配置
	 */
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:config:edit")
	public void edit(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			throw new ValidateException("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		configService.updateConfig(config);
	}

	/**
	 * 删除参数配置
	 */
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
	@SaCheckPermission("system:config:remove")
	public void remove(@PathVariable Long[] configIds) {
		configService.deleteConfigByIds(configIds);
	}

	/**
	 * 清空缓存
	 */
	@Log(title = "参数管理", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clearCache")
	@SaCheckPermission("system:config:remove")
	public void clearCache() {
		configService.clearCache();
	}
}
